package linestrategy

import (
	"codejy/analysis/lineanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"strings"
)

// 实例地址赋值调用策略
/**
 *  @#@xxx@#@.age=22
 */
type InstanceAddressPropAssignmentStrategy struct {
}

// 注册策略
func init() {
	RegisterStrategy(&InstanceAddressPropAssignmentStrategy{})
}

// 判断
func (f *InstanceAddressPropAssignmentStrategy) getId() int {
	return lineanalysis.InstanceAddressPropAssignment
}

func (f *InstanceAddressPropAssignmentStrategy) Execute(threadStore *structdef.ThreadStore) {
	content := threadStore.Code.LineContent
	split := strings.Split(content, "=")
	variableName := split[0]
	//获取=右边的表达式
	expression := split[1]
	//截取实例地址,,获取最后@#@位置
	index := strings.LastIndex(variableName, "@#@.")
	instanceAddress := variableName[:index+3]
	//获取类型属性名称
	propName := variableName[index+4:]
	//解析表达式
	calculateExpression := globalfunc.ExecuteFunc("AssignmentStrategyMode", threadStore, expression).(string)
	var variableType = globalfunc.ExecuteFunc("CodeAnalysis", threadStore, calculateExpression).(int)
	prop := store.SetInstanceProp(threadStore, instanceAddress, structdef.NewVarDefinitionValue(propName, calculateExpression, variableType))
	if !prop {
		panic("错误::::::实例属性" + content + "不存在")
	}
}
